<!--
 * @Author: zjf
 * @Date: 2023-11-30 14:27:54
 * @LastEditors: zjf
 * @LastEditTime: 2023-11-30 16:50:19
 * @Description: 纳统投资明细
-->
<template>
  <div>
    <l-query :span="8" :items="queryItems" :labelWidth="80" :height.sync="queryHeight" :formData="queryData"
      style="padding: 0 8px" :hasSearch="false" :hasReset="false">
      <template #year>
        <l-select v-model="queryData.year" :options='yearOptions' @change="handleSearch"></l-select>
      </template>
    </l-query>
    <l-table-edit ref="TableEdit" :tableColumns="columns" :tableData="tableData" @table="tableFunc"
      :showSummary="true"></l-table-edit>
  </div>
</template>

<script>
export default {
  props: {
    projectId: String,
  },
  data() {
    return {
      // 查询参数
      queryHeight: 40,
      queryData: {
        year: ''
      },
      columns: [
        { label: "年份", prop: "year", width: 80 },
        { label: "月份", prop: "month", width: 80 },
        { label: "当月纳统投资(万元)", prop: "currentMonthInvestmentAmount", width: 130, isSummary: true },
        { label: "下月预计纳统投资(万元)", prop: "nextMonthEstimatedInvestmentAmount", width: 150 },
        { label: "填报者", prop: "createUserId", dataType: "createuser" },
        { label: "更新时间", prop: "modifyTime", dataType: "modifytime" },

      ],
      queryItems: [
        { label: "投资年份", prop: "year", },
      ],

      // table参数
      tableData: [],
      tableField: {
        year: "",
        month: "",
        currentMonthInvestmentAmount: "",
        nextMonthEstimatedInvestmentAmount: "",
        createUserId: "",
        modifyTime: "",
        isEditing: true,
      },

      yearOptions: [{ label: "全部", value: "" }],
    };
  },
  computed: {},
  created() { },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getTableData()
      this.getYearList()
    },
    handleSearch() {
      this.getTableData()
    },

    /** 获取列表信息 */
    async getTableData() {
      let queryData = this.$deepClone(this.queryData)
      queryData.projectId = this.projectId
      const api = window.$crud("project/national/investment/list");
      const data = await this.$awaitWraper(api.getInfo(queryData));
      this.tableData = []
      if (data.length) {
        this.tableData = data.map(t => {
          return {
            ...t,
            isEditing: false,
          }
        });
      }
    },

    /** 获取投资年份列表 */
    async getYearList() {
      const api = window.$crud("project/national/investment/year/list");
      const data = await this.$awaitWraper(api.getInfo({ projectId: this.projectId }));
      if (data.length) {
        this.yearOptions = data.map(t => ({ label: t, value: t })).sort((a, b) => b.value - a.value)
        this.yearOptions.unshift({ label: "全部", value: "" })
      }
    },

    //#region table 事件

    tableFunc(res) {
      switch (res.type) {
        case 'save':
          this.handleSave(res.data)
          break;
        case 'delete':
          // 删除
          this.handleDelete(res)
          break;
        case 'push':
          // 新增一行
          this.tableData.unshift(this.tableField);
          break;
        default:
          break;
      }
    },

    /** 删除 */
    handleDelete(scope) {
      var index = this.tableData.indexOf(scope.data)
      this.$confirm(this.$t('是否删除该条数据?'), this.$t('提示'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(async () => {
        if (scope.data.id) {
          const api = window.$crud("project/national/investment");
          await api.remove(scope.data.id)
        }
        this.$message({
          type: 'success',
          message: this.$t('删除成功!')
        })
        this.tableData.splice(index, 1);

      }).catch(() => {
        this.$message({
          type: 'info',
          message: this.$t('已取消删除!')
        })
      })
    },
    /** 保存 */
    async handleSave(data) {
      var res = null
      data.projectId = this.projectId
      if (data.id) {
        // 编辑
        res = await this.$awaitWraper(window.$crud('project/national/investment').update(data.id, data));
      } else {
        // 新增
        res = await this.$awaitWraper(window.$crud('project/national/investment/info').add(data));
      }
      if (res) {
        this.$refs.TableEdit.handleCloseEdit(data) // 表格 关闭编辑
        this.$message({
          type: "success",
          message: '保存成功',
        });
      }
    },
    //#endregion
  }
};
</script>
<style lang="scss" scoped></style>